/* *********************************************************
 Interrupts declaration
 ********************************************************* */
void IRQ_Routine(void) __attribute__ ((interrupt("IRQ")));
void FIQ_Routine(void) __attribute__ ((interrupt("FIQ")));
void SWI_Routine(void) __attribute__ ((interrupt("SWI")));
void UNDEF_Routine(void) __attribute__ ((interrupt("UNDEF")));

/**********************************************************
 Header files
 **********************************************************/
#include "system.h"
#include "display/lcd.h"
#include "display/spi_lcd.h"
/**********************************************************
 MAIN
 **********************************************************/

void initialize(void) {

	// Init System clock, PLL and MAM
	system_Init();

	// Init the pearl and its leds
	pearl_Init();
	pearl_TestLights();
	pearl_WhiteLight(PEARL_LIGHT_ON);

	// Init LCD using software SPI
	lcd_Init();
	lcd_ClearScreen(COLOR_WHITE);

}

int main(void) {
	Triangle t_up,t_down,t_left,t_right;
	Rect r_center;
	Line line;
	unsigned char i;

	unsigned int colors[] = {COLOR_RED,COLOR_GREEN,COLOR_BLUE};

	// Initialize the system
	initialize();

	// init Triangles
	t_up.p1.x = 64;
	t_up.p1.y = 10;
	t_up.p2.x = 52;
	t_up.p2.y =	22;
	t_up.p3.x = 76;
	t_up.p3.y =	22;

	r_center.p1.x = 52;
	r_center.p1.y = 24;
	r_center.p2.x = 76;
	r_center.p2.y = 48;

	t_down.p1.x = 64;
	t_down.p1.y = 62;
	t_down.p2.x = 52;
	t_down.p2.y = 50;
	t_down.p3.x = 76;
	t_down.p3.y = 50;

	t_left.p1.x = 38;
	t_left.p1.y = 36;
	t_left.p2.x = 50;
	t_left.p2.y = 24;
	t_left.p3.x = 50;
	t_left.p3.y = 48;

	t_right.p1.x = 90;
	t_right.p1.y = 36;
	t_right.p2.x = 78;
	t_right.p2.y = 24;
	t_right.p3.x = 78;
	t_right.p3.y = 48;


	gpu2D_FillTriangle(&t_up,COLOR_WHITE);
	gpu2D_FillTriangle(&t_down,COLOR_WHITE);
	gpu2D_FillTriangle(&t_right,COLOR_WHITE);
	gpu2D_FillTriangle(&t_left,COLOR_WHITE);
	gpu2D_DrawTriangle(&t_up,COLOR_BLACK);
	gpu2D_DrawTriangle(&t_down,COLOR_BLACK);
	gpu2D_DrawTriangle(&t_right,COLOR_BLACK);
	gpu2D_DrawTriangle(&t_left,COLOR_BLACK);

	while (1) {
		if (pearl_IsMovingDown()) {
			gpu2D_FillTriangle(&t_down,COLOR_GREEN);
			delay_ms(100);
			gpu2D_FillTriangle(&t_down,COLOR_WHITE);
			gpu2D_DrawTriangle(&t_down,COLOR_BLACK);
		}
		if (pearl_IsMovingUp()) {
			gpu2D_FillTriangle(&t_up,COLOR_RED);
			delay_ms(100);
			gpu2D_FillTriangle(&t_up,COLOR_WHITE);
			gpu2D_DrawTriangle(&t_up,COLOR_BLACK);
		}
		if (pearl_IsMovingLeft()) {
			gpu2D_FillTriangle(&t_left,COLOR_BLUE);
			delay_ms(100);
			gpu2D_FillTriangle(&t_left,COLOR_WHITE);
			gpu2D_DrawTriangle(&t_left,COLOR_BLACK);
		}
		if (pearl_IsMovingRight()) {
			gpu2D_FillTriangle(&t_right,COLOR_BLACK);
			delay_ms(100);
			gpu2D_FillTriangle(&t_right,COLOR_WHITE);
			gpu2D_DrawTriangle(&t_right,COLOR_BLACK);
		}
		if (pearl_IsCenterPressed()) {
			gpu2D_FillRect(&r_center,COLOR_YELLOW);
			delay_ms(100);
			gpu2D_FillRect(&r_center,COLOR_WHITE);
		}
//		delay_ms(50);
	}

/*
	while(1) {
		lcd_ClearScreen(COLOR_WHITE);
		//delay_ms(1200);
		lcd_ClearScreen(COLOR_RED);
		//delay_ms(1200);
		lcd_ClearScreen(COLOR_GREEN);
		//delay_ms(1200);
		lcd_ClearScreen(COLOR_BLUE);
		//delay_ms(1200);
	}

	while (1) {
		pearl_WhiteLight(1);
		delay_ms(1000);
		pearl_WhiteLight(0);
		delay_ms(1000);
	}*/

	while (1)
		;
	return 0;
}

/*  Stubs for various interrupts (may be replaced later)  */
/*  ----------------------------------------------------  */
void IRQ_Routine(void) {
	while (1)
		;
}
void FIQ_Routine(void) {
	while (1)
		;
}
void SWI_Routine(void) {
	while (1)
		;
}
void UNDEF_Routine(void) {
	while (1)
		;
}
